package com.brt.museum.module;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.apache.shiro.subject.Subject;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.lang.Strings;
import org.nutz.mvc.Mvcs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.GET;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;

import com.brt.museum.bean.auth.User;
import com.brt.museum.service.auth.IShiroUserService;
import com.brt.museum.vo.Consts.SessionKeys;
import com.brt.museum.vo.MenuNode;
import com.dgj.DefaultDESKey;
import com.dgj.nutz.module.base.AbstractBaseModule;
import com.dgj.utils.encode.DES;
import com.dgj.utils.entries.Result;

/**
 * @author 王贵源
 * @email kerbores@dgj.com
 *
 *        create at 2015年10月20日 上午10:50:28
 */
@At("system")
public class SystemModule extends AbstractBaseModule {

	@Inject
	IShiroUserService shiroUserService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.dgj.nutz.module.base.AbstractBaseModule#_getNameSpace()
	 */
	@Override
	public String _getNameSpace() {
		return "system";
	}

	/**
	 * 登录界面
	 * 
	 * @return
	 */
	@At
	@GET
	@Filters
	@Ok("vel:login/login.html")
	public Result login() {
		String dgj = _getCookie("brt_museum");
		String userName = null;
		String password = null;
		boolean rememberMe = false;
		if (!Strings.isBlank(dgj)) {
			String info = DES.decrypt(dgj, DefaultDESKey.getKey());
			userName = info.split(":")[0];
			password = info.split(":")[1];
			rememberMe = true;
		}
		if (!rememberMe) {
			_addCookie("dgj_titans", DES.encrypt(userName + ":" + password, DefaultDESKey.getKey()), 0);
		}
		return Result.success().addData("userName", userName).addData("password", password).addData("rememberMe", rememberMe ? "checked" : "").addData("flag", rememberMe);
	}

	/**
	 * 登出
	 * 
	 * @param session
	 * @return
	 */
	@At
	@Filters
	public Result logout(HttpSession session) {
		Subject subject = SecurityUtils.getSubject();
		subject.logout(); // session 会销毁，在SessionListener监听session销毁，清理权限缓存
		if (log.isDebugEnabled()) {
			log.debug("用户退出登录");
		}
		return Result.success();
	}

	/**
	 * 登录提交
	 * 
	 * @param userName
	 * @param password
	 * @param rememberMe
	 * @return
	 */
	@At
	@POST
	@Filters
	public Result login(String userName, String password, boolean rememberMe) {
		Result result = shiroUserService.login(userName, password);
		if (result.isSuccess()) {
			User user = (User) result.getData().get("loginUser");
			_putSession(SessionKeys.SESSION_USER_KEY, result.getData().get("loginUser"));
			List<MenuNode> menuNodes = (List<MenuNode>) Mvcs.getReq().getSession().getAttribute(SessionKeys.SESSION_MENU_KEY);// 获取菜单
			if (menuNodes == null) {
				menuNodes = shiroUserService.getMenu(user.getId());
				_putSession(SessionKeys.SESSION_MENU_KEY, menuNodes);
			}
			if (rememberMe) {
				_addCookie("brt_museum", DES.encrypt(userName + ":" + password, DefaultDESKey.getKey()), 2 * 7 * 24 * 3600);
			}
		}
		return result;
	}

	/**
	 * 登录之后的主页
	 * 
	 * @return
	 */
	@At
	@Ok("vel:system/main.html")
	@RequiresUser
	public Result main() {
		return Result.success();
	}
}
